package com.e1xp.deport_management_system.controller;

import com.e1xp.deport_management_system.entity.Order;
import com.e1xp.deport_management_system.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author E1xP@foxmail.com
 * @version 1.0
 * @PACKAGE_NAME com.e1xp.deport_management_system.controller
 * @CLASS_NAME IndexController
 * @Description TODO 主页控制器
 * @Date 2021/4/24 下午 4:12
 **/
@Controller
@RequestMapping("/index")
public class IndexController {

    @Resource
    IProductService productService;

    @Resource
    IOrderService orderService;

    @Resource
    IRackService rackService;

    @Resource
    IVendorsService vendorsService;

    @Resource
    IIoService ioService;

    /**
     * 渲染首页数据
     *
     * @return ModelAndView
     */
    @RequestMapping("/frontData")
    public ModelAndView frontData(@SessionAttribute long instanceId) {
        ModelAndView modelAndView = new ModelAndView();
        //获取系列数据
        long productCount = productService.getCount(instanceId);//库存总量统计
        long orderCount = orderService.getUnFinishCount(instanceId);//待完成订单统计
        long rackCount = rackService.getCount(instanceId);//货架统计
        long vendorsCount = vendorsService.getCount(instanceId);//获取经销商统计
        List<Order> orderList = orderService.getRecent(instanceId, 5);//获取最近10条未处理订单
        List<Map<String, String>> ioList = ioService.getRecent(instanceId, 5);//获取最近10条入库记录
        //置入数据
        modelAndView.addObject("productCount", productCount);
        modelAndView.addObject("orderCount", orderCount);
        modelAndView.addObject("rackCount", rackCount);
        modelAndView.addObject("vendorsCount", vendorsCount);
        modelAndView.addObject("orderList", orderList);
        modelAndView.addObject("ioList", ioList);
        modelAndView.setViewName("frontPage");
        return modelAndView;
    }
}
